<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of articulos
 *
 * @author emontoro
 */
class ebarrio extends CI_Controller {

	function ebarrio() {
		parent::__construct();

		$this -> load -> database();
		$this -> load -> model('registro');

	}

	function index() {
		// Cargo el helper de url, con funciones para trabajo con URL del sitio
		$this -> load -> helper('url');

		// Variables de usuario logado o no
		include $_SERVER['DOCUMENT_ROOT'] . '/application/includes/acceso.inc.php';

	}

	function recupera_usuario() {
		$usuario = $this -> tank_auth -> get_username();
		echo $usuario;
	}

	function registro() {
		$data['publickey'] = $this -> config -> item('captcha_publickey_v3');
		$cuerpo = $this -> load -> view('registro_v.php', $data, true);
		echo $cuerpo;
	}

	function registrar() {
		$username = $this -> input -> post('email');
		$email = $this -> input -> post('email');
		$cod_postal = $this -> input -> post('cod_postal');
		$password1 = $this -> input -> post('password1');
		$password2 = $this -> input -> post('password2');

		$error = 0;
		// Se inicializa el erro a 0. Indica que es correcto

		$email_activation = $this -> config -> item('email_activation', 'tank_auth');

		if (!is_null($data = $this -> tank_auth -> create_user($username, $email, $password1, $email_activation))) {// success
			$data['site_name'] = $this -> config -> item('website_name', 'tank_auth');
		} else {
			$errors = $this -> tank_auth -> get_error_message();
			foreach ($errors as $k => $v)
				$data['errors'][$k] = $this -> lang -> line($v);

			$datajson['error_message'] = $data;

			$error = 1;
			// Se produce algún error
		}
		// Se comprueban si ya existe en alguna tabla

		$datajson['coderror'] = $error;
		echo json_encode($datajson);
	}

	function login() {
		$email = $this -> input -> post('email');
		$username = $this -> input -> post('email');
		$password = $this -> input -> post('password');
		$recordar = $this -> input -> post('recordar');

		$data['login_by_username'] = ($this -> config -> item('login_by_username', 'tank_auth') AND $this -> config -> item('use_username', 'tank_auth'));
		$data['login_by_email'] = $this -> config -> item('login_by_email', 'tank_auth');

		$error = 0;
		// Se inicializa el erro a 0. Indica que es correcto

		if ($this -> tank_auth -> login($username, $password, $recordar, $data['login_by_username'], $data['login_by_email'])) {
			// success
			
			$datajson['username'] = $this -> tank_auth -> get_username();
			
			$last_url = $this -> session -> flashdata('last_url');

		} else {
			$datajson['textoerror'] = 'Error validacion';
			$error = 1;
			$errors = $this -> tank_auth -> get_error_message();
			if (isset($errors['banned'])) {
				//// banned user
				$this -> _show_message($this -> lang -> line('auth_message_banned') . ' ' . $errors['banned']);
				$error = 1;
				//return;
			} elseif (isset($errors['not_activated'])) {// not activated user
				$error = 2;
				//                redirect('/auth/send_again/');
			} else {// fail
				$error = 3;
				foreach ($errors as $k => $v)
					$data['errors'][$k] = $this -> lang -> line($v);
			}
		}


   		if ($this -> tank_auth -> is_logged_in()) {
			//$data['user_id'] = $this->tank_auth->get_user_id();
			$data['username1'] = $this -> tank_auth -> get_username();
			$data['username2'] = 'Salir';

			$data['titulo1'] = 'Gestionar cuenta';
			$data['titulo2'] = 'Salir de la sesión';

			$data['data_menu_login_1'] = 'mantener';
			$data['data_menu_login_2'] = 'salir';

		} else {
			$data['username1'] = 'Entrar';
			$data['username2'] = 'Registrarse';

			$data['titulo1'] = 'Identificarse';
			$data['titulo2'] = 'Darse de alta';

			$data['data_menu_login_1'] = 'entrar';
			$data['data_menu_login_2'] = 'registrar';

		}



		$datajson['coderror'] = $error;
		$datajson['datos'] = $data;

		echo json_encode($datajson);
	}

	function validar_captcha() {

		$recaptcha_challenge_field = $this -> input -> post('recaptcha_challenge_field');
		$recaptcha_response_field = $this -> input -> post('recaptcha_response_field');

		require_once ("/recaptcha/recaptchalib.php");

		$privatekey = $this -> config -> item('captcha_privatekey');

		$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $recaptcha_challenge_field, $recaptcha_response_field);

		$error = $resp -> error;

		if (!$resp -> is_valid) {

			echo $resp -> error;
		} else {
			echo "success";
		}
	}

}
?>
